
import {newProduct, resolveValidTime} from "../productUtils.js";
import {tr} from "../../ux/translations.js";
import * as sources from "../sources.js";
import {buildWW3PrimaryWaves} from "./ww3-primary.js";
import {scalarProduct} from "../scalarProduct.js";
import {length} from "../../util/math.js";
import {ø} from "../../util/objects.js";
import * as utc from "../../util/utc.js";
import {createUnitDescriptors} from "../../util/units.js";
import {gaiaPath} from "../gaia.js";
import {createHTSGWPalette} from "../../palette/htsgw.js";
import {createPrimaryWavesPalette} from "../../palette/primaryWaves.js";


let palettes = {};

function ww3Path(attr, type) {
    const {time_cursor} = attr;
    const validTime = resolveValidTime(time_cursor, {hour: 3});
    const dir = time_cursor === "current" ? "current" : utc.print(validTime, "{YYYY}/{MM}/{DD}");
    const stamp = time_cursor === "current" ? "current" : utc.print(validTime, "{hh}{mm}");
    const file = `${stamp}-${type}-wave-30m.epak`;
    return gaiaPath("data/ww3", dir, file);
}

function newWW3Product(attr) {
    const validTime = resolveValidTime(attr.time_cursor, {hour: 3});
    const stepper = step => function() {
        return resolveValidTime(utc.add(this.validTime(), step), {hour: 3});
    };
    return {
        sourceHTML: sources.ww3,
        validTime: () => ø(validTime),
        prev2: stepper({day:  -1}),
        prev1: stepper({hour: -3}),
        next1: stepper({hour: +3}),
        next2: stepper({day:  +1}),
        navLabels: {
            prev2: `-1 ${tr`day`}`,
            prev1: `-3 ${tr`hours`}`,
            next1: `+3 ${tr`hours`}`,
            next2: `+1 ${tr`day`}`,
        },
    };
}

export function createPrimaryWavesLayer(attr) {
    return ø(newProduct(), newWW3Product(attr), {
        type: "primary_waves",
        descriptionHTML: {
            name: tr`Peak Wave Period`,
            qualifier: "",
        },
        paths: [ww3Path(attr, "primary")],
        builder: buildWW3PrimaryWaves,
        unitDescriptors: createUnitDescriptors({
            "s": {scalarize: length, precision: 1, convention: "into"},
        }),
        scale: palettes.primaryWaves ??= createPrimaryWavesPalette(),
        particles: {velocityScale: 1/612, maxIntensity: 12, waves: true},
    });
}

export function createSignificantWaveHeightLayer(attr) {
    const selector = /Significant_height/;  // "Significant_height_of_combined_wind_waves_and_swell_surface"
    return ø(newProduct(), newWW3Product(attr), {
        type: "significant_wave_height",
        descriptionHTML: {
            name: tr`Significant Wave Height`,
            qualifier: "",
        },
        paths: [ww3Path(attr, "sig_height")],
        builder: file => scalarProduct(file, selector, {hasMissing: true}),
        unitDescriptors: createUnitDescriptors({
            "m":  {convert: x => x,                   precision: 2},
            "ft": {convert: x => x * 100 / 2.54 / 12, precision: 1},
        }),
        scale: palettes.htsgw ??= createHTSGWPalette(),
    });
}
